<?php
  class BaseController extends JController
  {
    var $_name = null;
    
    function display()
    {
      $document =& JFactory::getDocument();
      
      $view = & $this->getView($this->_name, $document->getType());
      
      if ($model = & $this->getModel($this->_name)) 
      {
        $view->setModel($model, true);
      }                               
      
      $view->displayList();   
    }
    
    function edit()
    {
      $cids = JRequest::getVar('cid', null, 'default', 'array' ); 
    
      if($cids === null)
      {
                JError::raiseError(500, 'cid parameter missing from the request');
      }    
    
      $document =& JFactory::getDocument();
      
      $view = & $this->getView($this->_name, $document->getType());
      
      if ($model = & $this->getModel($this->_name))
      {
        $view->setModel($model, true);
      }                               
      
      $view->displayEdit();  
    }
    
    function save()
    {
      $data = JRequest::get('POST');
       
      $model = & $this->getModel($this->_name); 
      
      $model->save($data);
       
      $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&c='.JRequest::getVar('c'), false);
      $this->setRedirect($redirectTo,  ucfirst($this->_name) . ' Saved!');             
    }
    
    function add()
    {
      $document =& JFactory::getDocument();
      
      $view = & $this->getView($this->_name, $document->getType());
      
      if ($model = & $this->getModel($this->_name)) 
      {
        $view->setModel($model, true);
      }                               
      
      $view->displayAdd();    
    }
    
    function remove()
    {
             $ids = JRequest::getVar('cid', null, 'default', 'array' ); 
         
             if($ids === null)
       { 
         JError::raiseError(500, 'cid parameter missing from the request');
             }
         
             $model = & $this->getModel($this->_name);
             $model->delete($ids);
         
             $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&c='.JRequest::getVar('c'), false);
             $this->setRedirect($redirectTo, ucfirst($this->_name) . '(s) Deleted!');                
        }    
    
    function publish()
    {
      $ids = JRequest::getVar('cid', null, 'default', 'array' ); 
       
      $model = & $this->getModel($this->_name); 
      $model->publish($ids, true);
      $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&c='.JRequest::getVar('c'), false);
      $this->setRedirect($redirectTo, ucfirst($this->_name) . '(s) Published!');   
    }
    
    function unpublish()
    {
      $ids = JRequest::getVar('cid', null, 'default', 'array' ); 
       
      $model = & $this->getModel($this->_name); 
      $model->publish($ids, 0);
      $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&c='.JRequest::getVar('c'), false);
      $this->setRedirect($redirectTo, ucfirst($this->_name) . '(s) Unpublished!');       
    }
  }
?>